package com.ehome.poct.core.dao.mybatis;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.order.PromotionBundlePriceProfitConfDTO;
import com.ehome.poct.core.dao.entity.order.PromotionBundlePriceProfitConfDO;
import com.ehome.poct.core.dao.mapping.PromotionBundlePriceProfitConfMapping;
import com.ehome.poct.core.dao.mybatis.mapper.order.PromotionBundlePriceProfitConfMapper;
import com.ehome.poct.core.repository.order.PromotionBundlePriceProfitConfRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 捆绑促销活动 价格体系，分润体系配置表RepositoryImpl
 *
 * @author muqiu
 * @Date 2022-07-19 17:43
 */
@Repository
public class PromotionBundlePriceProfitConfRepositoryImpl extends AbstractCrudRepository<PromotionBundlePriceProfitConfDO, PromotionBundlePriceProfitConfDTO> implements PromotionBundlePriceProfitConfRepository {
    @Resource
    PromotionBundlePriceProfitConfMapper mapper;
    @Resource
    PromotionBundlePriceProfitConfMapping mapping;

    @Override
    public List<PromotionBundlePriceProfitConfDTO> getValidBundlerPriceInfos(Long priceProfitSchemeId) {
        LocalDateTime now = LocalDateTime.now();
        List<PromotionBundlePriceProfitConfDO> list = mapper.selectList(Wrappers.<PromotionBundlePriceProfitConfDO>lambdaQuery()
                .eq(PromotionBundlePriceProfitConfDO::getTestStripPriceProfitSchemeId, priceProfitSchemeId));
        List<PromotionBundlePriceProfitConfDO> validList = list.stream().filter(f -> now.isAfter(f.getEffectiveStart()) && now.isBefore(f.getEffectiveEnd())).collect(Collectors.toList());
        return mapping.to(validList);
    }
}
